// share.js
const HOST = require('../../config.js').host
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar:'',
    nickName:'',
    iptClass: '',
    iptClass1: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    wx.login({
      success: function success() {
        wx.getUserInfo({
          success: function success(res) {
            // app.userInfo = res.userInfo;

            self.setData({
              avatar: res.userInfo.avatarUrl,
              nickName: res.userInfo.nickName
            });
          }
        });
      }
    });
    //获取本地缓存登录信息
    self.autoLogin();
  },
  autoLogin:function() {
    //获取本地缓存登录信息
    let channelID, enterpriseID;
    try {
      var value = wx.getStorageSync('channelID')
      if (value) {
        channelID = value;
      }
    } catch (e) {
      console.log("初次登陆！")
    }
    try {
      var value = wx.getStorageSync('enterpriseID')
      if (value) {
        enterpriseID = value;
      }
    } catch (e) {
      console.log("初次登陆！")
    }
    if (channelID && enterpriseID) {
      this.loginRequest(channelID, enterpriseID, '渠道ID或企业ID有改动！');
    } 
  },
  onShow:function() {
    //获取本地缓存登录信息
    // var backpage = getCurrentPages()[1];
  },
  loginSubmit: function (e) {
    let channelID = e.detail.value.channelID.replace(/\s/ig, '');
    let enterpriseID = e.detail.value.enterpriseID.replace(/\s/g, "");
    let that = this;
    if (!channelID) {
      wx.showToast({
        title: '渠道ID不能为空！',
        image: '../../assets/images/error.png',
        mask: true
      });
      this.setData({
        iptClass: 'erroClass'
      })
      return false;
    } else if (!enterpriseID) {
      wx.showToast({
        title: '企业ID不能为空！',
        image: '../../assets/images/error.png',
        mask: true
      })
      this.setData({
        iptClass1: 'erroClass'
      })
      return false;
    }
    //提交登陆请求
    this.loginRequest(channelID, enterpriseID, '请输入正确的渠道ID,正确的企业ID！');
  },
//提交登陆请求
  loginRequest: function (channelID, enterpriseID,PromptErrorMsg) {
    let self = this;
    wx.request({
      url: `${HOST}v1/qrcode/findByCompanyIdAndCompanyKey/${channelID}/${enterpriseID}`, //登陆请求
      method: 'GET',
      success: function (res) {
        //账号信息验证成功状态
        let info = JSON.stringify({
          channelID: channelID,
          enterpriseID: enterpriseID
        });        
        if (res.data.sysCode === 1 && res.data.data) return ( 
          wx.setStorage({
            key: 'channelID',
            data: channelID,
          }),
          wx.setStorage({
            key: 'enterpriseID',
            data: enterpriseID,
          }),
          wx.redirectTo({
            url: '../charts/line/line?statistics=' + info,
          })
        )
        //登陆成功跳转数据统计页面

        wx.showToast({
          title: PromptErrorMsg,
          image: '../../assets/images/error.png',
          mask: true
        });
      },
      fail: function (res) {
        if (res.data.sysCode === 0) {
          wx.showToast({
            title: '请检查网络是否正常...',
            icon: 'loading',
            mask: true

          })
        }
    self.autoLogin();        
      }
    })
  },
  removeErroClass: function (e) {
    if (e.currentTarget.id == "channelID") {
      this.setData({
        iptClass: ''
      })
    } else {
      this.setData({
        iptClass1: ''
      })
    }

  }
})